﻿//获取用户输入的装备参数
function getObject() {

	// 获取输入的属性
	var selecteds = $("#propDiv [selected]=selected");

	var props = new Array();
	for ( var i = 0; i < selecteds.length; i++) {
		if ($(selecteds[i]).val() == '0')
			continue;
		props.push($(selecteds[i]).val());
	}

	if (props.length == 0) {
		showMessage(alertStrings.needInputData);
		return null;
	}

	// 重复校验
	for ( var i = 0; i < props.length; i++) {
		for ( var j = i + 1; j < props.length; j++) {
			if (props[i] == props[j]) {
				showMessage(alertStrings.repeatProp);
				return null;
			}
		}
	}

	var object = null;

	var type = getType();
	object = getZero(type);

	// 获取值
	var selectedOptions = $("#propDiv [selected]=selected");
	var value = 0;
	var intValue = 0;
	var inputElement = null;
	var selectedOption = null;
	for ( var i = 0; i < selectedOptions.length; i++) {
		selectedOption = selectedOptions[i];

		if ($(selectedOption).val() == '0')
			continue; // 没选择

		inputElement = $(selectedOption).parent().parent().next().find("input");
		value = inputElement.val();

		if (value == 0 || value == '' || value == '0') {
			// 没有输入值
			showMessage(alertStrings.emptyProp,function(){inputElement.focus();});
			return null;
		}

		// 是否为正确的数字
		try {
			intValue = parseInt(value);
			if (isNaN(intValue)) {
				showMessage(alertStrings.needANum,function(){inputElement.focus();});
				return null;
			}
		} catch (e) {
			showMessage(alertStrings.needANum,function(){inputElement.focus();});
			return null;
		}

		// 是否为大于0的数字
		if (intValue <= 0) {
			showMessage(alertStrings.needGreatZero,function(){inputElement.focus();});
			return null;
		}

		var propName = $(selectedOption).val();

		// 是否超过最大值
		if (getMaxValue(type, propName) < intValue) {
			showMessage(alertStrings.overrideMax,function(){inputElement.focus();});
			return null;
		}

		// overrideMax

		// 给object 赋值
		// var a = $(selectedOption).parent().parent().find("input").val();

		eval("object." + propName + "=" + intValue + ";");
	}

	return object;
}

// 显示评价
function show() {
	var type = getType();

	var object = getObject();

	if (object == null) {
		return;
	}

	// 开始判断装备属性
	var systemResults = new Array();

	var propCount = 0; // 参数个数
	var mainProp = null; // 主属性名称
	var mainPropValue = 0; // 主属性值
	var over60Count = 0; // 大于等于属性极值50的属性的个数
	var over80Count = 0; // 大于等于属性极值75的属性的个数
	var fullCount = 0; // 等于属性极值的属性的个数
	var hotPropArray = new Array(); // 热门属性数组
	var specialPropArray = new Array(); // 特殊属性数组
	for (prop in object) {
		var value = getValue(object, prop);
		var maxValue = getMaxValue(type, prop);
		if (value == 0)
			continue;

		if (value == maxValue) {
			fullCount++;
		} else if (value / 4 * 5 >= maxValue) {
			over80Count++;
		} else if (value / 3 * 5 >= maxValue) {
			over60Count++;
		}

		var specialMax = getVar(type + CONSTANTS.VAR_NAME_SPECIAL_MAX);

		if (isHaveProp(specialMax, prop)) {
			specialPropArray.push(getShowPropName(prop));
		}

		if (isHaveProp(hotProps, prop)) {
			hotPropArray.push(getShowPropName(prop) + "(" + value + ")");
		}

		propCount++;
	}

	var maxData = getVar(type + CONSTANTS.VAR_NAME_MAX);

	systemResults.push("小黑认为:");

	// 属性个数
	systemResults.push("此装备有条" + propCount + "属性");

	// 属性质量
	if (fullCount != 0) {
		systemResults.push(fullCount + "条<label style='color:"+CONSTANTS.PROP_COLOR.full+"' title='等于极值'>满属性</label>");
	}
	if (over80Count != 0) {
		systemResults.push(over80Count + "条<font color='"+CONSTANTS.PROP_COLOR.over80+"'><span title='大于等于极值的80%'>极佳</span></font>的属性");
	}
	if (over60Count != 0) {
		systemResults.push(over60Count + "条<font color='"+CONSTANTS.PROP_COLOR.over60+"'><span title='大于等于极值的60%'>及格</span></font>的属性");
	}

	// 主属性
	var mainPropName = getMainPropName(object);

	if (mainPropName == null) {
		systemResults.push("此装备没有主属性");
	} else {
		systemResults.push("此装备主属性为:"
				+ getShowPropName(mainPropName)
				+ "["
				+ getValue(object, mainPropName)
				+ "/"
				+ getValue(maxData, mainPropName)
				+ "],得分:"
				+ parseInt(getValue(object, mainPropName)
						/ getValue(maxData, mainPropName) * 100) + ".");
	}
	/*
	 * if (fullCount == propCount) { systemResults.push("此装备太逆天了！全是满属性！"); }
	 * else if (fullCount + over80Count == propCount) {
	 * systemResults.push("此装备已经非常不错了，目测大海景。"); } else if (fullCount +
	 * over80Count + over60Count == propCount) {
	 * systemResults.push("此装备不错，让吧里的人看看吧"); } else {
	 * systemResults.push("我还不能评价此装备好坏，不好意思。"); }
	 */

	// 特殊属性个数
	if (specialPropArray.length != 0) {
		systemResults.push("另外，此装备包含"
				+ connectArray(specialPropArray, "&nbsp;,") + ""
				+ specialPropArray.length + "个特殊属性！");
	}

	// 热门属性
	if (hotPropArray.length != 0) {
		systemResults.push("此外，此装备包含" + connectArray(hotPropArray, " &nbsp;,")
				+ "" + hotPropArray.length + "个当下热门的属性!");
	}

	// 得分
	var commitLink = "<a target='_blank' href='"+CONSTANTS.DEVELOP_LINK+"'>提供装备评分数据</a>" ;
	var modifyLink = "<a target='_blank' href='"+CONSTANTS.DEVELOP_LINK+"'>更正数据</a>" ;
	try {
		var scoreData = getVar(type + CONSTANTS.VAR_NAME_SCORE_DATA);
		systemResults.push("此装备得分:" + getScore(maxData, scoreData, object));
		
		var scoreDetailStr = getScoreDetail(maxData,scoreData,object);
		var scoreDataStr = getObjectPropValueStr(scoreData, ",");
		
		$("#scoreDataDiv").html(scoreDataStr);
		$("#scoreDetailDiv").html(scoreDetailStr);
	}catch(ex) {
		alert(ex);
		log(ex);
		systemResults.push("小黑没有此类型装备的评分数据,你可以"+commitLink);
	}
	
	// 显示
	showSystemResult(systemResults);

	// 评价数据
	/*
	 * var goodDataResults = new Array();
	 * 
	 * try{ var goodDatas = getVar(type + CONSTANTS.VAR_NAME_GOOD_DATAS);
	 * 
	 * var goodData = null; for ( var i = 0; i < goodDatas.length; i++) {
	 * goodData = goodDatas[i]; if (checkObjectByGoodData(object, goodData)) {
	 * goodDataResults.push(goodData); } } showGoodDataResult(goodDataResults);
	 * }catch(ex) { goodDataResults.push("还没有吧友评价数据"); }
	 * 
	 * showGoodDataResult(goodDataResults);
	 */
	// 拍卖数据
	/*
	 * var ahDataResults = new Array();
	 * 
	 * var ahDatas = getVar(type + CONSTANTS.VAR_NAME_AH_DATAS);
	 * 
	 * 
	 * var ahData = null; for ( var i = 0; i < ahDatas.length; i++) { ahData =
	 * ahDatas[i]; if (checkTheObject(object, ahData)) {
	 * ahDataResults.push(ahData); } }
	 * 
	 * showAhDataResult(ahDataResults);
	 */
}

function checkObjectByGoodData(object,goodData) {
	if(object==null || goodData ==null) {
		return false ;
	}
	
	for(prop in goodData) {
		if(isHaveProp(CONSTANTS.GOOD_DATA_DEFAULTS,prop)) {
			continue ;
		}
		
		if(getValue(goodData,prop)>getValue(object,prop)) {
			return false ;
		}
	}	
	return true ;
}

// 显示评价结果
function showGoodDataResult(goodDataResults) {
	if (goodDataResults == null || goodDataResults.length == 0) {
		return;
	}
	
	$("#goodDataResultDiv").html("");

	var str = "";

	var goodData = null;
	for ( var i = 0; i < goodDataResults.length; i++) {
		goodData = goodDataResults[i];
		if(!goodData.id) {
			str+=goodData;
			continue;
		}
		str += "["
				+ (i+1)
				+ "]&nbsp;&nbsp;&nbsp;"
				+ goodData.author
				+ "认为:"
				+ goodData.description
				+ "<a href='javascript:void(0);' onclick='javascript:showGoodData("
				+ goodData.id + ");'>查看</a><p/>";
	}
	
	$("#goodDataResultDiv").append(str);

	$("#goodDataResultTr").fadeIn();
}

// 显示AH结果
function showAhDataResult(ahDataResults) {

}

// 获取分数
function getScore(maxData, scoreData, object) {
	var score = 0;
	var propScore = 0;
	var propValue = 0;
	var propMaxValue = 0;
	var mainPropName = "";

	var mainPropName = getMainPropName(object); // 主属性名称

	for (prop in scoreData) {
		propScore = getValue(scoreData, prop);
		propMaxValue = getValue(maxData, prop);

		if (prop == 'mainProp' && mainPropName != null) {
			propMaxValue = getValue(maxData, mainPropName);
			propValue = getValue(object, mainPropName);
		} else if (isHaveProp(object, prop)) {
			propValue = getValue(object, prop);
			propMaxValue = getValue(maxData, prop);
		} else {
			continue;
		}

		score += propScore * (propValue / propMaxValue);
	}
	return parseInt(score);
}

// 获取分数
function getScoreDetail(maxData, scoreData, object) {
	var scoreDetial = "";
	var propScore = 0;
	var propValue = 0;
	var propMaxValue = 0;
	var mainPropName = "";

	var mainPropName = getMainPropName(object); // 主属性名称

	for (prop in scoreData) {
		propScore = getValue(scoreData, prop);
		propMaxValue = getValue(maxData, prop);

		if (prop == 'mainProp' && mainPropName != null) {
			propMaxValue = getValue(maxData, mainPropName);
			propValue = getValue(object, mainPropName);
		} else if (isHaveProp(object, prop)) {
			propValue = getValue(object, prop);
			propMaxValue = getValue(maxData, prop);
		} else {
			continue;
		}
		
		if(propValue!=0) {
			scoreDetial += getShowPropName(prop)+":" ;
			scoreDetial += propScore * (propValue / propMaxValue);
			scoreDetial += "<br />" ;
		}
	}
	return scoreDetial;
}

// 服务器发生改变
function changeServer() {
	// alert(getServer());
}

function getServer() {
	return $("#serverSelect [selected]=selected").val();
}

function getType() {
	return $("#typeSelect [selected]=selected").val();
}

// 显示结果
function showResult(results) {
	$("#resultDiv").html("");
	$("#resultTr").fadeIn();

	if (results.length == 0) {
		$("#resultDiv").append("还没有吧友对这类属性的装备进行过评价.");
		return;
	}

	for ( var i = 0; i < results.length; i++) {
		result = results[i];
		$("#resultDiv")
				.append(
						"<div>"
								+ result.author
								+ "于"
								+ getDateString(result.date)
								+ "在"
								+ result.server
								+ "卖掉了属性与此件装备相似的装备(<a href='javascript:void(0);' onclick=javascript:showGoodData('"
								+ result.id + "')>点击查看属性</a>),价格" + result.gold
								+ "W</div>");
	}
}

// 显示系统辨识结果
function showSystemResult(systemResults) {

	$("#resultDiv").append("<div id='systemResultDiv'><div>");
	$("#systemResultDiv").html("");
	var systemResult = null;
	$("#systemResultTr").fadeIn();
	for ( var i = 0; i < systemResults.length; i++) {
		systemResult = systemResults[i];
		$("#systemResultDiv").append(systemResult + "<p>");
	}
}

// 检查此物品是否能匹配估价数据
function checkTheObject(object, goodData) {
	var value = null;
	var checkValue = null;
	var prop = null;
	for (prop in goodData) {

		if (prop == 'author' || prop == 'server' || prop == 'date'
				|| prop == 'gold' || prop == 'id')
			continue;

		eval("checkValue=goodData." + prop);
		eval("value=object." + prop);

		if (value >= checkValue) {
			continue;
		} else {
			return null;
		}
	}
	return goodData;
}

function showGoodData(id) {
	var type = getType();
	var goodDatas = getVar(getType()+CONSTANTS.VAR_NAME_GOOD_DATAS);

	var result = null;
	for ( var i = 0; i < goodDatas.length; i++) {
		if (goodDatas[i].id == id) {
			result = goodDatas[i];
			break;
		}
	}

	if (result == null) {
		showMessage("没有找到ID为" + id + "的数据");
		return;
	}

	var str = getObjectPropValueStr(result, ",");
	showMessage(str);
}

function viewPropDescription() {
	showMessage("[xx]为有较高属性值的属性,例如胸甲加的体力上限为300.\n*开头的为稀有属性,例如:手套的'击中昏迷'。");
}


function clear() {
	var needClears = ["#scoreDataDiv ,#scoreDetialDiv","#systemResultDiv"] ;
	for(var i = 0;i<needClears.length;i++) {
		$(needClears[i]).html('');
	}
}

var maxVar = null;

/* 物品类型发生改变时 */
function changeType() {

	var type = getType();

	clear();
	
	$("#propDiv").html("");
	if (type == '0') {
		return;
	}
	addProp();
	addProp();
	addProp();
}

/* 属性发生改变时 */
function changeProp(element) {
	
	var type = getType();
	
	var propName = $(element).val();

	if (propName == '0') {
		return;
	}

	var maxVar = getVar(type + CONSTANTS.VAR_NAME_MAX);
	var maxPropValue = getValue(maxVar,propName);
	
	$(element).parent().parent().find(".max").html(maxPropValue);
	if(propName == 'ignoreTheDurability'){
		$(element).parent().parent().find("input").val(maxPropValue);
	}
	changePropValue($(element).parent().parent().find("input"));
	if ($(element).parent().parent().parent().parent().next().find("select").length == 0) {
		addProp();
	}
}

/* 增加一个属性 */
function addProp() {
	var type = getType();
	
	var specialMaxVar = getVar(type + CONSTANTS.VAR_NAME_SPECIAL_MAX);
	var maxVar = getVar(type + CONSTANTS.VAR_NAME_MAX);
	
	var propName = null;
	var showPropName = null;
	var appendStr = "<table><tbody><tr><td><select class='porpSelect'  onchange='javascript:changeProp(this);'><option value='0'>请选择</option>";
	for (prop in maxVar) {
		showPropName = getShowPropName(prop);

		var specialHave = null;
		var greatHave = null;

		eval("greatHave=" + type + "GreatMax." + prop);
		eval("specialHave=" + type + "SpecialMax." + prop);

		if (specialHave) {
			appendStr += "<option value='" + prop + "'>*" + showPropName
					+ "</option>";
		} else if (greatHave) {
			appendStr += "<option value='" + prop + "'>[" + showPropName
					+ "]</option>";
		} else {
			appendStr += "<option value='" + prop + "'>" + showPropName
					+ "</option>";
		}
	}
	appendStr += "</select></td>" ;
	appendStr += "<td><input class='propValue' type='text' onkeydown='javascript:changePropValue(this);' /></td>" ;
	appendStr += "<td align='left'><div class='max'/></td>";
	appendStr += "<td style='width:100px;background-color:#cccccc;' width=100><div class='evaluate' /></td>" ;
	appendStr += "</tr></tbody></table>";
	$("#propDiv").append(appendStr);
}

// 显示版权
function showAboutAuthor() {
	showMessage(aboutAuthor);
}
// 切换显示
function toggleShow(element, cascadeElement) {
	$(cascadeElement).slideToggle();
	if ('[隐藏]' == $(this).html()) {
		$(this).html('[显示]');
	} else {
		$(this).html('隐藏');
	}
}

// 属性值发生改变
function changePropValue(inputElement) {
	
	setTimeout(function(){	
		var value = $(inputElement).val();
		var propName = $(inputElement).parent().parent().find("select").val();
		
		// 未选择属性
		if(propName == '0') {
			return ;
		}
		
		var maxValue = getValue(getVar(getType()+CONSTANTS.VAR_NAME_MAX),propName);
		var color = getValue(CONSTANTS.PROP_COLOR,getEvaluate(value,maxValue));
		var evaluateElement = $(inputElement).parent().parent().find(".evaluate");

		var percent = Math.min(value/maxValue*100,100);
		evaluateElement.animate({ 
			    width:percent+"px"
			  }, CONFIG.PERCENT_BAR_CHANGE_SPEED );
		evaluateElement.css("backgroundColor",color);
		evaluateElement.parent().attr("title",percent+"%");
	},CONFIG.PERCENT_BAR_CHANGE_SPEED) ;
}

function getEvaluate(value,maxValue) {
	if(value>=maxValue) {
		return 'full' ;
	} else if (value / 4 * 5 >=maxValue) {
		return 'over80' ;
	} else if (value / 3 * 5 >=maxValue) {
		return 'over60' ;
	} else {
		return 'little' ;
	}
}

function togetherDevelop() {
	showMessage(togetherDevelopInfo);
}